<script setup lang="ts">
import { onLoad } from '@dcloudio/uni-app'
import { ref } from 'vue';
import { PostAddressData, PostAddress, PutMemberAddress } from '@/apis/address'
import { GetMemberAddress } from '@/apis/address';

//新增收获地址
onLoad(({ id }) => {
  uni.setNavigationBarTitle({
    title: id ? '修改地址' : '新建地址',
  })
})

const form = ref({} as PostAddressData)
const submitForm = async () => {
  if (addressId.value) {
    await PutMemberAddress(addressId.value, form.value)
  } else {
    await PostAddress(form.value)
  }
  uni.navigateBack({ delta: 1 })
  uni.showToast({
    title: addressId ? '修改成功' : '保存成功',
    icon: 'success',
    mask: true
  })
}
const fullLocation = ref<string | undefined>()
//省 /市 / 县
const regionChange = (e: any) => {
  const [provinceCode, cityCode, countyCode] = e.detail.code
  // form.value.provinceCode = provinceCode
  // form.value.cityCode = cityCode
  // form.value.countyCode = countyCode

  //用户列表显示
  fullLocation.value = (e.detail.value as string[]).join('')
  Object.assign(form.value, { provinceCode, cityCode, countyCode })
}
const isDefaultChange = (e: any) => {
  form.value.isDefault = e.detail.value ? 1 : 0
}

//修改收货地址
const addressId = ref()
onLoad(async ({ id }) => {
  if (id) {
    addressId.value = id
    const res = await GetMemberAddress(addressId.value)
    Object.assign(form.value, res)
  }
})
</script>

<template>
  <view class="viewport">
    <!-- 地址信息 -->
    <view class="form">
      <view class="form-item">
        <text class="label">姓名</text>
        <input v-model="form.receiver" placeholder-style="color: #888" placeholder="请填写收货人姓名" />
      </view>
      <view class="form-item">
        <text class="label">手机号码</text>
        <input v-model="form.contact" placeholder-style="color: #888" placeholder="请填写收货人手机号码" />
      </view>
      <view class="form-item">
        <text class="label">省/市/县 (区)</text>
        <picker mode="region" @change="regionChange">
          <view class="region" v-if="fullLocation">{{ fullLocation }}</view>
          <view class="placeholder" v-else>请填写收货人所在城市</view>
        </picker>
      </view>
      <view class="form-item">
        <text class="label">详细地址</text>
        <input v-model="form.address" placeholder-style="color: #888" placeholder="街道、楼牌号信息" />
      </view>
      <view class="form-item">
        <text class="label">设置默认地址</text>
        <switch color="#27ba9b" @change="isDefaultChange" :checked="form.isDefault === 1" />
      </view>
    </view>
    <!-- 提交按钮 -->
    <view class="button" @click="submitForm">保 存</view>
  </view>
</template>

<style>
page {
  background-color: #f4f4f4;
}

.form {
  margin: 20rpx 20rpx 0;
  padding: 0 20rpx;
  border-radius: 10rpx;
  background-color: #fff;
}

.form .form-item {
  display: flex;
  min-height: 96rpx;
  line-height: 46rpx;
  padding: 25rpx 10rpx;
  background-color: #fff;
  font-size: 28rpx;
  border-bottom: 1rpx solid #ddd;
  position: relative;
}

.form .form-item:last-child {
  border: none;
}

.form .form-item .label {
  width: 200rpx;
  color: #333;
}

.form .form-item input {
  flex: 1;
  display: block;
  height: 46rpx;
}

.form .form-item switch {
  position: absolute;
  right: -10rpx;
  transform: scale(0.7) translateY(-8px);
}

.form .form-item picker {
  flex: 1;
}

.form .form-item .region {
  color: #333;
}

.form .form-item .placeholder {
  color: #888;
}

.button {
  height: 80rpx;
  text-align: center;
  line-height: 80rpx;
  margin: 30rpx 20rpx;
  color: #fff;
  border-radius: 80rpx;
  font-size: 30rpx;
  background-color: #27ba9b;
}
</style>
